package com.xuyiwei.xblog.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.xuyiwei.xblog.business.system.result.notice.out.NoticeDto;
import com.xuyiwei.xblog.business.system.result.user.out.UserDto;
import com.xuyiwei.xblog.config.server.WebsocketServer;
import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.common.annotion.BussinessLog;
import com.xuyiwei.xblog.core.common.constant.dictmap.NoticeMap;
import com.xuyiwei.xblog.core.common.constant.factory.PageFactory;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.log.LogObjectHolder;
import com.xuyiwei.xblog.core.shiro.ShiroKit;
import com.xuyiwei.xblog.core.util.Convert;
import com.xuyiwei.xblog.core.util.StringUtil;
import com.xuyiwei.xblog.core.util.ToolUtil;
import com.xuyiwei.xblog.business.system.model.Menu;
import com.xuyiwei.xblog.business.system.model.Notice;
import com.xuyiwei.xblog.business.system.service.INoticeService;
import com.xuyiwei.xblog.core.util.WebSocketUtil;
import com.xuyiwei.xblog.modular.system.warpper.NoticeWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 通知控制器
 *
 * @author fengshuonan
 * @Date 2017-05-09 23:02:21
 */
@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseController {

    private String PREFIX = "/system/notice/";

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private WebsocketServer websocketServer;

    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "notice.html";
    }


    /**
     * 管理员发送群体页面
     * @return
     */
    @RequestMapping(value = "/send")
    public String send(Model model) {
        //加1的原因是因为进入这个页面自己也要算进在线人数，因此先加1
        model.addAttribute("online", websocketServer.getOnlineUserCount()+1);
        return PREFIX+"websocketSend.html";
    }

    /**
     * 发送管理员消息
     * @return
     */
    @RequestMapping(value = "/sengAdminMsg")
    @ResponseBody
    public Result<Boolean> sengAdminMsg(String msg){
        Result<Boolean> result = Result.instance();
        try{
            WebSocketUtil.sendNotificationMsg(msg, websocketServer.getOnlineUsers(),websocketServer.getOnlineVistors());
            result.setData(true);
        }catch (Exception e){
            result.setError("500","发送消息失败");
        }
        return result;
    }

    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result<List<NoticeDto>> list(String condition) {
        Result<List<NoticeDto>> result = Result.instance();
        try{
            Page<NoticeDto> page = new PageFactory<NoticeDto>().defaultPage();
            List<NoticeDto> list = this.noticeService.findPageByCondition(page,condition, page.getOrderByField(), page.isAsc());
            List<Map> maps = (List<Map>) super.warpObject(new NoticeWrapper(list));
            result.setData((List<NoticeDto>)Convert.convertListMap(NoticeDto.class,  maps));
        }catch (Exception e){
            result.setError("500","获取通知列表出错") ;
        }
        return result;
    }


    /**
     * 删除通知
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除通知",key = "noticeId",dict = NoticeMap.class)
    public Result<Boolean> delete(@RequestParam(required = true, value = "ids[]") List<Long> ids) {
        Result<Boolean> result = Result.instance();
        try{
            List<Notice> list = this.noticeService.selectBatchIds(ids);
            StringBuffer name = new StringBuffer();
            for(Notice notice:list){
                //缓存被删除的部门名称
                if(ToolUtil.isNotEmpty(notice)){
                    name = StringUtil.append(name,notice.getTitle());
                }
            }

            LogObjectHolder.me().set(name.toString());
            this.noticeService.deleteNoticesByIds(ids);
            result.setData(true);
        }catch (Exception e){
            result.setError("500","删除通知失败");
        }
        return result;
    }


    /**
     * 清空通知
     */
    @RequestMapping(value = "/deleteAll")
    @ResponseBody
    public Result<Boolean> deleteAll( ) {
        Result<Boolean> result = Result.instance();
        try{
            this.noticeService.deleteAllNotice();
            result.setData(true);
        }catch (Exception e){
            result.setError("500","清空通知失败");
        }
        return result;
    }

}
